/*******************************************************************************
 * Package: com.hngtrust.iir.point.controller
 * Type:    PointController
 * Date:    2021/12/10 2:54 下午
 *
 * Copyright (c) 2021 -------- -------- CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hngtrust.iir.point.controller;


import com.hngtrust.clickhouse.point.dto.PointSentimentRespDTO;
import com.hngtrust.clickhouse.point.dto.PointsCodeReqDTO;
import com.hngtrust.clickhouse.point.dto.QuerySentimentByPointIdDTO;
import com.hngtrust.clickhouse.point.entity.AppPoint;
import com.hngtrust.clickhouse.point.entity.AppPointSentiment;
import com.hngtrust.clickhouse.point.service.AppPointSentimentService;
import com.hngtrust.clickhouse.point.service.AppPointService;
import com.hngtrust.framework.bind.ResponseInfo;
import com.hngtrust.framework.security.util.SecurityUtils;
import com.hngtrust.iir.base.entity.IndustrySw;
import com.hngtrust.iir.base.service.IndustrySwService;
import com.hngtrust.iir.constant.Constant;
import com.hngtrust.iir.point.controller.dto.*;
import com.hngtrust.iir.user.entity.CollectInfo;
import com.hngtrust.iir.user.entity.TagRel;
import com.hngtrust.iir.user.entity.User;
import com.hngtrust.iir.user.service.CollectInfoService;
import com.hngtrust.iir.user.service.TagRelService;
import com.hngtrust.iir.util.HtmlUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 专家观点
 *
 * @author Chaidd
 * @date 2021/12/10 2:54 下午
 */
@Slf4j
@Api(tags = "专家观点")
@RestController
@RequestMapping("/point")
public class PointController {

    @Autowired
    private IndustrySwService industrySwService;

    @Autowired
    private AppPointService appPointService;

    @Autowired
    private AppPointSentimentService appPointSentimentService;

    @Value("${tiger.minio.out.address}")
    private String minioAddress;

    @Autowired
    private TagRelService tagRelService;

    @Autowired
    private CollectInfoService collectInfoService;


    @ApiOperation("根据行业code查询二级行业类型的专家观点")
    @PostMapping("/queryTwoNumPoints-code")
    public ResponseInfo<List<PointsCodeRespVO>> queryTwoPointsByNumCode(@ApiParam(value = "行业code") @Valid @RequestBody PointsCodeReqVO vo) {
        log.info("PointController.queryTwoPointsByNumCode start...");
        List<PointsCodeReqDTO> pointsCodeReqDTOList = appPointService.queryTwoByIndustryCode(vo.getCode().substring(0,2));
        IndustrySw industrySw = new IndustrySw();
        industrySw.setCode(vo.getCode().substring(0,2));
        industrySw.setLevel(2);
        List<IndustrySw> industrySwList = industrySwService.querySubList(industrySw);
        return getListResponseInfo(pointsCodeReqDTOList, industrySwList);
    }

    private ResponseInfo<List<PointsCodeRespVO>> getListResponseInfo(List<PointsCodeReqDTO> pointsCodeReqDTOList, List<IndustrySw> industrySwList) {
        List<PointsCodeRespVO> pointsCodeRespVOList = getPointsCodeRespVOS(pointsCodeReqDTOList,industrySwList);
        log.info("********pointsCodeRespVOList的大小为***********："+pointsCodeRespVOList.size());
        if(!pointsCodeRespVOList.isEmpty()){
            pointsCodeRespVOList = pointsCodeRespVOList.stream().sorted((b, a) -> -a.getIndustryId().compareTo(b.getIndustryId())).collect(Collectors.toList());

        }
        return new ResponseInfo<>(pointsCodeRespVOList);
    }

    @ApiOperation("根据行业code查询三级行业类型的专家观点")
    @PostMapping("/queryThreeNumPoints-code")
    public ResponseInfo<List<PointsCodeRespVO>> queryThreePointsByIndustryCode(@ApiParam(value = "行业code") @Valid @RequestBody PointsCodeReqVO vo) {
        log.info("PointController.queryThreeNewsByIndustryCode start...");
        List<PointsCodeReqDTO> pointsCodeReqDTOList = appPointService.queryThreeByIndustryCode(vo.getCode().substring(0,4));
        IndustrySw industrySw = new IndustrySw();
        industrySw.setCode(vo.getCode().substring(0,4));
        industrySw.setLevel(3);
        List<IndustrySw> industrySwList = industrySwService.querySubList(industrySw);
        return getListResponseInfo(pointsCodeReqDTOList, industrySwList);
    }

    @ApiOperation("根据行业code查询四级行业类型的专家观点")
    @PostMapping("/queryFourNumPoints-code")
    public ResponseInfo<List<PointsCodeRespVO>> queryFourPointsByIndustryCode(@ApiParam(value = "行业code") @Valid @RequestBody PointsCodeReqVO vo) {
        log.info("PointController.queryFourNewsByIndustryCode start...");
        List<PointsCodeReqDTO> pointsCodeReqDTOList = appPointService.queryFourByIndustryCode(vo.getCode().substring(0,6));
        IndustrySw industrySw = new IndustrySw();
        industrySw.setCode(vo.getCode().substring(0,6));
        industrySw.setLevel(4);
        List<IndustrySw> industrySwList = industrySwService.querySubList(industrySw);
        List<PointsCodeRespVO> pointsCodeRespVOList = getPointsCodeRespVOS(pointsCodeReqDTOList,industrySwList);
        log.info("********pointsCodeRespVOList的大小为***********："+pointsCodeRespVOList.size());
        if(!pointsCodeRespVOList.isEmpty()){
            pointsCodeRespVOList = pointsCodeRespVOList.stream().sorted((b, a) -> -a.getCreateTime().compareTo(b.getCreateTime())).collect(Collectors.toList());

        }
        return new ResponseInfo<>(pointsCodeRespVOList);
    }

    @ApiOperation("查询行业大类的专家观点")
    @PostMapping("/queryAllPoints-Industry")
    public ResponseInfo<List<PointsCodeRespVO>> queryAllPointsIndustry() {
        log.info("PointController.queryAllPointsIndustry start...");
        List<PointsCodeReqDTO> pointsCodeReqDTOList = appPointService.queryAllIndustry();
        IndustrySw industrySw = new IndustrySw();
        industrySw.setLevel(1);
        List<IndustrySw> industrySwList = industrySwService.querySubListByOneLevel(industrySw);
        return getListResponseInfo(pointsCodeReqDTOList, industrySwList);
    }

    //查询行业分类及其数量方法
    private List<PointsCodeRespVO> getPointsCodeRespVOS(List<PointsCodeReqDTO> pointsCodeReqDTOList, List<IndustrySw> industrySwList) {
        List<PointsCodeRespVO> pointsCodeRespVOList = new ArrayList<>();
        List<String> industryCode = new ArrayList<>();
        for(PointsCodeReqDTO pointsCodeReqDTO : pointsCodeReqDTOList){
            industryCode.add(pointsCodeReqDTO.getIndustryCode());
        }
        for(IndustrySw indestrySw : industrySwList){
            if(!industryCode.contains(indestrySw.getCode())){
                PointsCodeRespVO pointsCodeRespVO = new PointsCodeRespVO();
                IndustrySw industrySw = industrySwService.queryByCode(indestrySw.getCode());
                pointsCodeRespVO.setPointsNum(0);
                pointsCodeRespVO.setIndustryCode(indestrySw.getCode());
                pointsCodeRespVO.setIndustryId(industrySw.getId());
                pointsCodeRespVO.setIndustryName(industrySw.getName());
                pointsCodeRespVO.setCreateTime(industrySw.getCreateTime());
                pointsCodeRespVOList.add(pointsCodeRespVO);
            }
        }

        for(PointsCodeReqDTO pointsCodeReqDTO : pointsCodeReqDTOList){
            PointsCodeRespVO pointsCodeRespVO = new PointsCodeRespVO();
            IndustrySw industrySw = industrySwService.queryByCode(pointsCodeReqDTO.getIndustryCode());
            pointsCodeRespVO.setPointsNum(pointsCodeReqDTO.getPointsNum());
            pointsCodeRespVO.setIndustryCode(pointsCodeReqDTO.getIndustryCode());
            pointsCodeRespVO.setIndustryId(industrySw.getId());
            pointsCodeRespVO.setIndustryName(industrySw.getName());
            pointsCodeRespVO.setCreateTime(industrySw.getCreateTime());
            pointsCodeRespVOList.add(pointsCodeRespVO);
        }

        return pointsCodeRespVOList;
    }

    @ApiOperation("根据id查询")
    @PostMapping("/query-id")
    public ResponseInfo<ListPointRespVO> queryById(@ApiParam(value = "公告id") @Valid @RequestBody QueryByPointIdReqVO vo) {
        AppPoint appPoint = appPointService.queryById(vo.getId());
        ListPointRespVO respDTO = new ListPointRespVO();
        if(appPoint != null){
            BeanUtils.copyProperties(appPoint,respDTO);
        }
        if (appPoint.getHtml()!=null && !appPoint.getHtml().isEmpty()){
            String s = HtmlUtils.replaceHtmlTag(appPoint.getHtml(), minioAddress);
            if (s.indexOf("&lt;")!= -1 && s.indexOf("&gt;")!=-1){
                s.replaceAll("&gt;",">").replaceAll("&lt;","<");
            }
            respDTO.setHtml(s);
        }
        respDTO.setEmotionProport(appPoint.getEmotionProport().setScale(2, RoundingMode.HALF_UP));

        TagRel tagRel = new TagRel();
        tagRel.setDataType(Constant.TAG_DATATYPE_NEWS);
        tagRel.setDataId(appPoint.getId());
        List<TagRel> tagRels = tagRelService.queryList(tagRel);

        AppPointSentiment sentiment = new AppPointSentiment();
        sentiment.setNewsId(appPoint.getId());

        if(null != appPoint.getSentiment()){
            if (Constant.SENTIMENT_POSITIVE.equals(appPoint.getSentiment())) {
                respDTO.setSentiment("积极");
            }
            if (Constant.SENTIMENT_NEUTRAL.equals(appPoint.getSentiment())) {
                respDTO.setSentiment("中立");
            }
            if (Constant.SENTIMENT_NEGATIVE.equals(appPoint.getSentiment())) {
                respDTO.setSentiment("消极");
            }
        }
        respDTO.setLabels(tagRels);
        List<QuerySentimentByPointIdDTO> dtos = appPointSentimentService
                .querySentimentByPointId(appPoint.getId());

        respDTO.setCompNames(dtos);
        respDTO.setInfoType(2);
        //是否已收藏
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        CollectInfo collect = new CollectInfo();
        collect.setInfoId(Long.getLong(vo.getId()));
        collect.setCreateUserId(loginUser.getId());
        List<CollectInfo> collectList = collectInfoService.queryList(collect);
        if(!collectList.isEmpty()){
            respDTO.setCollect(Constant.WHETHER_YES);
        }
        else{
            respDTO.setCollect(Constant.WHETHER_NO);
        }
        return new ResponseInfo<>(respDTO);
    }

    @ApiOperation("查询情感色彩的行业资讯")
    @PostMapping("/queryPoint-sentiment")
    public ResponseInfo<List<PointSentimentRespVO>> queryPointSentiment() {
        log.info("PointController.queryNewsSentiment start...");
        List<PointSentimentRespDTO> pointSentimentRespDTOList = appPointService.queryPointSentiment();
        List<PointSentimentRespVO> pointSentimentRespVOS = new ArrayList<>();
        if(pointSentimentRespDTOList.size() > 0){
            for(PointSentimentRespDTO pointSentimentRespDTO : pointSentimentRespDTOList){
                PointSentimentRespVO pointSentimentRespVO = new PointSentimentRespVO();
                BeanUtils.copyProperties(pointSentimentRespDTO,pointSentimentRespVO);
                if (Constant.SENTIMENT_POSITIVE.equals(pointSentimentRespDTO.getSentimentCode())) {
                    pointSentimentRespVO.setSentimentName("积极");
                }
                if (Constant.SENTIMENT_NEUTRAL.equals(pointSentimentRespDTO.getSentimentCode())) {
                    pointSentimentRespVO.setSentimentName("中立");
                }
                if (Constant.SENTIMENT_NEGATIVE.equals(pointSentimentRespDTO.getSentimentCode())) {
                    pointSentimentRespVO.setSentimentName("消极");
                }
                pointSentimentRespVOS.add(pointSentimentRespVO);
            }
        }

        return new ResponseInfo<>(pointSentimentRespVOS);
    }


}
